<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="二维码编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入二维码编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否导出" prop="isExport">
        <template>
             <el-select v-model="queryParams.isExport" placeholder="请选择">
            <el-option
              v-for="item in isExportOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
             </el-option>
            </el-select>
        </template>
      </el-form-item>
      <el-form-item label="是否已绑定物体" prop="isBinding">
        <template>
             <el-select v-model="queryParams.isBinding" placeholder="请选择">
            <el-option
              v-for="item in isBindingOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
             </el-option>
            </el-select>
        </template>
      </el-form-item>
      <el-form-item >
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="twoDimensionalCodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" align="center" prop="id" />
      <el-table-column label="二维码编码" align="center" prop="code" width="360" />
      <el-table-column label="是否导出" align="center" prop="isExport" >
        <template slot-scope="scope">
           {{getisExportType(scope.row.isExport)}}
        </template>
    </el-table-column>
      <el-table-column label="是否绑定物体" align="center" prop="isBinding" >
        <template slot-scope="scope">
           {{getisBingdingType(scope.row.isBinding)}}
        </template>
    </el-table-column>
      <el-table-column label="绑定物品名称" align="center" prop="bingGoodsName" width="200"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
           size="mini"
            type="text"
            icon="el-icon-edit"
           @click="handleView(scope.row)">查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:twoDimensionalCode:edit']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  <el-dialog
      title="查看二维码"
      :visible.sync="dialogVisible"
      width="350px"
      :before-close="handleClose"
      center
      :modal-append-to-body="false">
      <span>
       <img slot="reference" :src="fileUrl">
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listTwoDimensionalCode, getTwoDimensionalCode, delTwoDimensionalCode, addTwoDimensionalCode, updateTwoDimensionalCode, exportTwoDimensionalCode } from "@/api/labdata/twoDimensionalCode";

export default {
  name: "TwoDimensionalCodeManagement",
  data() {
    return {
      host:process.env.VUE_APP_BASE_API,
      fileUrl: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 二维码表格数据
      twoDimensionalCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        filePath: null,
        color: null,
        isExport: null,
        isBinding: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 条形码是否导出
       isExportOptions: [{
          value: '0',
          label: '否'
        }, {
          value: '1',
          label: '是'
        }],
        // 条形码是否绑定物体
       isBindingOptions: [{
          value: '0',
          label: '否'
        }, {
          value: '1',
          label: '是'
        }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询二维码列表 */
    getList() {
      this.loading = true;
      listTwoDimensionalCode(this.queryParams).then(response => {
        this.twoDimensionalCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        filePath: null,
        color: null,
        isExport: null,
        isBinding: null,
        bingGoodsName: null,
        bingdingTime: null,
        remark: null,
        createdTime: null,
        createdBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加二维码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTwoDimensionalCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改二维码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTwoDimensionalCode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTwoDimensionalCode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除二维码编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTwoDimensionalCode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有二维码数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTwoDimensionalCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //使用插槽
	  getisExportType(e) {
      for (var i = 0; i < this.isExportOptions.length; i++) {
        if (this.isExportOptions[i].value == e) {
          return this.isExportOptions[i].label;
        }
      }
    },

    //使用插槽
	 getisBingdingType(e) {
      for (var i = 0; i < this.isBindingOptions.length; i++) {
        if (this.isBindingOptions[i].value == e) {
          return this.isBindingOptions[i].label;
        }
      }
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.dialogVisible = true;
      this.fileUrl = this.host+'/labdata/twoDimensionalCode/imageDowload/image?id='+row.id
      console.log(this.fileUrl);
    },
     /** 取消查看按钮操作 */
    handleClose(done) {
      this.dialogVisible = false
      },
  }
};
</script>
